#ifndef _INVESTOR_ACCOUNT_H_
#define _INVESTOR_ACCOUNT_H_
#include "DataTypes.h"
#include "UserInfo.h"
#include "ServiceLib.h"

#include <boost/shared_ptr.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/shared_ptr.hpp>
#include "Portfolio.h"

//多点登录的用户只算一个;
class SERVICELIB_API InvestorAccount:public Portfolio,public UserInfo
{
public:
	InvestorAccount(const std::string& id);
	InvestorAccount(const std::string& InvestorID,const std::string& BrokerID);
	~InvestorAccount(void);
public:
	std::string GetField(int field);
	void setField(int field, const std::string& value);

	std::pair<std::string,std::string> GetInvestorIDBrokerID();

	void SafeUpdate(const Investor& );
	void SafeUpdate(TradingAccountSPtr ta );
	void SafeUpdate(const SettlementInfo& s,bool bIsLast=false );

	void SafeUpdate(const SettlementInfoConfirm& s, bool bIsLast = false);

	void SafeUpdate(InvestorPositionSPtr ptrPosition,bool bUserIsLogin = false);
	void SafeUpdate(InvestorPositionDetailSPtr,bool bUserIsLogin=false );
	void SafeUpdate(boost::shared_ptr<InvestorPositionCombineDetail>,bool bUserIsLogin=false );

	void SafeUpdate(boost::shared_ptr<InstrumentCommisionRate>, bool bUserIsLogin = false);
	void SafeUpdate(boost::shared_ptr<InstrumentMarginRate>, bool bUserIsLogin = false);

	void SafeUpdate(OrderSPtr, bool bUserIsLogin = false);
	void SafeUpdate(TradeSPtr, bool bUserIsLogin = false);
	void SafeUpdate(TickSPtr tick);
	void SafeUpdate(InputOrderSPtr pInputOrder);
	void SafeUpdate(InputOrderActionSPtr pInputOrder);
	void SafeUpdate(boost::shared_ptr<BrokerTradingParams> borkerTradingParam);

	boost::shared_ptr<Investor> GetInvestor();
	boost::shared_ptr<SettlementInfo> GetSettlementInfo();
	boost::shared_ptr<SettlementInfoConfirm> GetSettlementInfoConfirm();
	//获取持仓;
	std::vector<InvestorPositionDetailSPtr >& GetInvestorPositionDetail();
	std::vector<InvestorPositionCombineDetailSPtr >& GetInvestorComboPositionsDetail();
	//获取手续费和保证金率;
	virtual boost::shared_ptr<InstrumentCommisionRate> GetInstCommissionRate(const std::string& instId);
	boost::shared_ptr<InstrumentMarginRate> GetInstMarginRate(const std::string& instId);

	std::map<std::string, boost::shared_ptr<InstrumentCommisionRate> >& GetInstCommissionRates();
	std::map<std::string, boost::shared_ptr<InstrumentMarginRate> >& GetInstMarginRates();
protected:
	void Update(const Investor&);
	void Update(const SettlementInfo& s,bool bIsLast=false);
	void Update(InvestorPositionDetailSPtr,bool IsUserLogin=false);
	void Update(InvestorPositionCombineDetailSPtr,bool IsUserLogin=false);
	void Update(InvestorPositionSPtr ptrPosition);

	virtual boost::shared_ptr<BrokerTradingParams> GetBrokerTradingParams();
public:
	void LoadInstCommisionRate();
	void SaveInstCommisionRate();
	void LoadInstMarginRate();
	void SaveInstMarginRate();

	bool IsMyTrade(TradeSPtr trade);
protected:
	boost::mutex m_Mutex4Investor;
	boost::mutex m_Mutex4Account;
	boost::mutex m_Mutex4SettlementInfo;
	boost::mutex m_Mutex4Order;
	boost::mutex m_Mutex4Trade;
public:
	boost::mutex m_Mutex4PosDetail;
	boost::mutex m_Mutex4CmbPosDetail;
protected:
	//持仓明细;
	std::vector<boost::shared_ptr<InvestorPositionDetail> > m_InvestorPositionDetail;
	//组合持仓明细;
	std::vector<boost::shared_ptr<InvestorPositionCombineDetail> > m_InvestorPositionCombineDetail;
	//合约手续费;
	std::map<std::string,boost::shared_ptr<InstrumentCommisionRate> > m_InstCommisionRates;
	//保证金率;
	std::map<std::string, boost::shared_ptr<InstrumentMarginRate> > m_InstMarginRates;
protected:
	//投资者;
	boost::shared_ptr<Investor> m_Investor;
	//结算信息;
	boost::shared_ptr<SettlementInfo> m_SettlementInfo;
	//结算信息确认;
	boost::shared_ptr<SettlementInfoConfirm> m_SettlementInfoConfirm;
	//交易参数;
	boost::shared_ptr<BrokerTradingParams> m_BrokerTradingParams;

protected:
	//其他会话的报单;
	boost::shared_ptr<std::map<std::string, InputOrderSPtr > > m_OtherInputOrders;
	boost::shared_ptr<std::map<std::string, OrderSPtr > >  m_OtherOrders;
	boost::shared_ptr<std::map<std::string, TradeSPtr > >  m_OtherTrades;
	boost::shared_ptr<std::vector<InvestorPositionSPtr > > m_OtherPositions;

	bool m_bFilterMyOrder; //是否过滤非自己的报单信息,false表示不过滤,即其他会话的报单信息也会进行响应;

public:
	virtual void SetFilterMyOrder(bool bFilter);
	virtual bool IsFilterMyOrder() const;
	virtual UserInfo& GetUser();

	virtual bool IsMyOrder(OrderSPtr order);
};


#endif

